#!/bin/bash
#set -x
# while read line;do
#     eval "$line"
# done < ./build.conf
# id=`sed '/^ID=/!d;s/.*=//' urfile`
WORK_PATH=$(readlink -f ${0} | xargs dirname)
cfg_file=${WORK_PATH}/.arene_docs.conf
source ${cfg_file}
log_dir="${WORK_PATH}/log/$(date +%Y%m%d)"
hosting_index_path="${WORK_PATH}/index_bak/current_index"
api_index_path="${WORK_PATH}/index_bak/current_index/cockpitapi_draft"
ei_en_common="${WORK_PATH}/mkdocs-review/docs/InterfaceSpecificationDraft/Reference/InterfaceSpec/ApplicationServices/VehicleControl/ei-vhct-vehicle-control"
ei_jp_common="${WORK_PATH}/mkdocs-review/docs/InterfaceSpecificationDraft/jp/Reference/InterfaceSpec/ApplicationServices/VehicleControl/ei-vhct-vehicle-control"
ei_en_common_bak="${WORK_PATH}/code_bak/ei_common_bak/en/common"
ei_jp_common_bak="${WORK_PATH}/code_bak/ei_common_bak/jp/common"

function hosting_init
{
	cp ${hosting_index_path}/*md ${WORK_PATH}/mkdocs/docs/Reference/InterfaceSpec
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cp ${hosting_index_path}/*md ${WORK_PATH}/mkdocs/docs/Reference/InterfaceSpec."
	fi
	cp ${hosting_index_path}/mkdocs.yml ${WORK_PATH}/mkdocs
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cp ${hosting_index_path}/mkdocs.yml ${WORK_PATH}/mkdocs."
	fi
}

function cockpitapi_draft_init
{
	cp ${api_index_path}/index.md ${WORK_PATH}/arene-cockpit-api/build/docs
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cp ${api_index_path}/index.md ${WORK_PATH}/arene-cockpit-api/build/docs."
	fi
}

function update_ei_common
{
	if [[ ! -e ${ei_en_common}/common ]];then
		echo "[INFO ] cp -r ${ei_en_common_bak} ${ei_en_common}."
		cp -r ${ei_en_common_bak} ${ei_en_common}
	fi

	if [[ ! -e ${ei_jp_common}/common ]];then
		echo "[INFO ] cp -r ${ei_jp_common_bak} ${ei_jp_common}."
		cp -r ${ei_jp_common_bak} ${ei_jp_common}
	fi
}

function delete_hosting_dir
{
	rm -fr ./InterfaceSpecificationDraft/en
	rm -fr ./InterfaceSpecificationDraft/Appendix
	rm -fr ./InterfaceSpecificationDraft/Sample
	rm -fr ./InterfaceSpecificationDraft/jp/Appendix
	rm -fr ./InterfaceSpecificationDraft/jp/Sample
	rm -fr ./InterfaceSpecificationDraft/Guide/AreneLabTools
	rm -fr ./InterfaceSpecificationDraft/Guide/BestPractice
	rm -fr ./InterfaceSpecificationDraft/Guide/BuildRuntimeandSDK
	rm -fr ./InterfaceSpecificationDraft/Guide/CertificationProgram
	rm -fr ./InterfaceSpecificationDraft/Guide/CoreTopic
	rm -fr ./InterfaceSpecificationDraft/Guide/CreateHelloworldApp
	rm -fr ./InterfaceSpecificationDraft/Guide/CreateHelloworldService
	rm -fr ./InterfaceSpecificationDraft/Guide/DevelopEnvironmentSystemSpec
	rm -fr ./InterfaceSpecificationDraft/Guide/DevelopUsingeSDK
	rm -fr ./InterfaceSpecificationDraft/Guide/SetupRuntime
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/AreneLabTools
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/BestPractice
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/BuildRuntimeandSDK
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/CertificationProgram
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/CoreTopic
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/CreateHelloworldApp
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/CreateHelloworldService
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/DevelopEnvironmentSystemSpec
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/DevelopUsingeSDK
	rm -fr ./InterfaceSpecificationDraft/jp/Guide/SetupRuntime
	rm -fr ./InterfaceSpecificationDraft/Reference/Appendix/NotifyList
	rm -fr ./InterfaceSpecificationDraft/Reference/Appendix/UnitList
	rm -fr ./InterfaceSpecificationDraft/jp/Reference/Appendix/NotifyList
	rm -fr ./InterfaceSpecificationDraft/jp/Reference/Appendix/UnitList
	rm -fr ./InterfaceSpecificationDraft/Reference/WalkthroughDiagram
	rm -fr ./InterfaceSpecificationDraft/jp/Reference/WalkthroughDiagram
}

function build_cockpitapi_draft
{
	if [[ -e ${log_dir} ]]; then
        :> ${log_dir}/build_cockpitapi_draft.log
	else
		mkdir -p ${log_dir}
        :> ${log_dir}/build_cockpitapi_draft.log
	fi
	repo_src_abs_path=${WORK_PATH}/arene-cockpit-api
	if [[ -e ${repo_src_abs_path} ]] ; then

            docker run --rm -it -v ~:/home/hosthome:ro -v $(pwd):/workdir --name poky-$(id -un)-shenchh --workdir=/workdir \
                ${CAPNP_BUILD_DOCKER_NAME}:${CAPNP_BUILD_DOCKER_TAG} \
                    bash -c "cd /workdir/arene-cockpit-api/build/ ;\
                        bash build.sh ;\
                        cd .. ;\
                        chown -R 1002:1002 docs build ;\
                    exit" > ${log_dir}/build_cockpitapi_draft.log
        
            if [[ $?  -ne 0 ]]; then
                echo "[ERR ] # Failed to build capnp files to markdown files using capnp."
                return 1
            fi
	else
		echo "[ERR ] The capnp file path [${repo_src_abs_path}] does not exist!"
		return 1
	fi
}

function hosting_commit
{
	cd ~/work/arene_hosting/mkdocs-review/docs
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cd ~/work/arene_hosting/mkdocs-review/docs."
	fi
	rm -fr InterfaceSpecificationDraft
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to rm -fr InterfaceSpecificationDraft."
	fi
	mkdir InterfaceSpecificationDraft
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to mkdir InterfaceSpecificationDraft."
	fi

	echo "[INFO ] cp -fr ~/work/arene_hosting/mkdocs/site/* ./InterfaceSpecificationDraft/."
	cp -fr ~/work/arene_hosting/mkdocs/site/* ./InterfaceSpecificationDraft/
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cp -fr ~/work/arene_hosting/mkdocs/site/* ./InterfaceSpecificationDraft/."
	fi
	echo "[INFO ] update_ei_common start."
	update_ei_common
	echo "[INFO ] delete_hosting_dir start."
	delete_hosting_dir

	git add . >/dev/null
	git commit -m "update" >/dev/null
	git push >/dev/null
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to git push."
	else
		echo "[INFO ] git push SUCC."
		echo "Confirm Address: https://github.tmc-stargate.com/pages/qihua-tang/arene-cockpit-api_charging/InterfaceSpecificationDraft/Reference/InterfaceSpec/InterfaceSpec_index/"
	fi
}

function cockpitapi_draft_commit
{
	cd ~/work/arene_hosting/mkdocs-review/docs
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cd ~/work/arene_hosting/mkdocs-review/docs."
	fi
	# rm -f CockpitAPIDraft.tar.gz
	rm -fr CockpitAPIDraft
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to rm -fr CockpitAPIDraft."
	fi
	mkdir CockpitAPIDraft
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to mkdir CockpitAPIDraft."
	fi
	cp -fr ~/work/arene_hosting/arene-cockpit-api/docs/* ./CockpitAPIDraft/
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to cp -fr ~/work/arene_hosting/arene-cockpit-api/docs/* ./CockpitAPIDraft/."
	fi

	cd ~/work/arene_hosting/mkdocs-review/docs/CockpitAPIDraft
	rm -rf am/ en/ charging/ energy_monitor/ fuel_consumption/ media_bus_proxy/ seat/ template/ update_service/
	cd ~/work/arene_hosting/mkdocs-review/docs/CockpitAPIDraft/jp
	rm -rf am/ charging/ energy_monitor/ fuel_consumption/ media_bus_proxy/ seat/ template/ update_service/

	cd ~/work/arene_hosting/mkdocs-review/docs/
	# tar -zcvf CockpitAPIDraft.tar.gz CockpitAPIDraft

	git add .
	git commit -m "update"
	git push
	if [[ $? -ne 0 ]]; then
		echo "[ERR ] Failed to git push."
	else
		echo "[INFO ] git push SUCC."
		echo "Confirm Address: https://github.tmc-stargate.com/pages/qihua-tang/arene-cockpit-api_charging/CockpitAPIDraft/"
	fi
}

function reset_commit
{
	cd ~/work/arene_hosting/mkdocs-review/docs
	$ git reset --hard HEAD
	$ git clean -df
	$ git pull
}


######################################################
# main
######################################################
if [[ $# -ne 1 ]] ; then
    # echo "Line:${LINENO}"
    echo "[ERR ] Incorrect number of parameters! count=$#" 1>&2
    usage
    exit 1
else
    case ${1} in
        "api")
			# cockpitapi_draft_init
			echo "[INFO ] build_cockpitapi_draft start."
            time build_cockpitapi_draft
			echo "[INFO ] build_cockpitapi_draft SUCC."
			echo "[INFO ] cockpitapi_draft_commit start."
            time cockpitapi_draft_commit
            ;;
        "hosting")
			# hosting_init
            bash arene_docs.sh build-capnp
			bash arene_docs.sh build
			echo "[INFO ] hosting_commit start."
            time hosting_commit
            ;;
        "hosting-init")
			time hosting_init
            ;;
        "hosting-build")
			time bash arene_docs.sh build 
			echo "[INFO ] hosting_commit start."
            time hosting_commit
            ;;
        # "api-commit")
        #     time cockpitapi_draft_commit
        #     ;;
        # "hosting-commit")
		# 	echo "[INFO ] hosting_commit start."
        #     time hosting_commit
        #     ;;
        "reset-commit")
			echo "[INFO ] reset_commit start."
            time reset_commit
            ;;
    esac
fi
